import {
    Message
} from 'element-ui' //引用饿了么UI消息组件
export default function(app) {
    const axios = app.$axios
        // 基本配置
    axios.defaults.timeout = 10000
    axios.defaults.headers.post['Content-Type'] = 'application/json'
    axios.defaults.headers.patch['Content-Type'] = 'application/json'
    axios.defaults.headers.put['Content-Type'] = 'application/json'

    // 请求回调
    axios.onRequest((config) => {
        // console.log(app.$auth)
        // console.log(config)
        // console.log(process.server)
        if (!process.server) {
            if (window.sessionStorage.Token && window.sessionStorage.Token.length >= 128) { //store.state.token 获取不到值？？
                // 判断是否存在token，如果存在的话，则每个http header都加上token
                config.headers.Authorization = "Bearer " + window.sessionStorage.Token;
            } else {
                if (config.url !== '/login/token' && config.url !== '/login/user') {
                    Message({
                        message: '登录失效，请先登录',
                        type: 'warning',
                        duration: 2 * 1000
                    })
                }
            }
        }

    })

    // 返回回调
    axios.onResponse((res, error) => {
        //   if (res.headers.refreshtoken) {
        //     Cookie.set('token', res.headers.refreshtoken)
        //   }

        // console.log('onResponse')
        // console.log(res)
        // 保存token
        if (!process.server) {
            if (res.config.url === '/login/token' && res.data.data.isLogin) {
                window.sessionStorage.setItem('Token', res.data.data.token)
                window.sessionStorage.setItem('UserInfo', res.data.data.user)
            }
        }

        return Promise.resolve(res.data)
    })

    // 错误回调
    axios.onError((error) => {
        // console.log('onError')
        // console.log(app.$store.state)
        if (error.response && error.response.status) {
            switch (error.response.status) {
                case 400:
                    let data = error.response.data
                    Message({
                        message: data.detail,
                        type: 'error',
                        duration: 2 * 1000
                    })
                    break;
                case 500:
                    Message({
                        message: '服务器错误',
                        type: 'error',
                        duration: 2 * 1000
                    })
                    break;
                case 401:
                    if (location.pathname !== '/login') {
                        Message({
                            message: '登录超时，请重新登录',
                            type: 'warning',
                            duration: 2 * 1000
                        })
                        if (!process.server) {
                            window.sessionStorage.setItem('Token', '')
                            location.href = '/login'
                        }
                    }
                    break
                default:
                    break
            }
        } else {
            console.log(error)
        }

    })
}
